import Vue from "vue";
import Loading from "@/components/Loading-index.vue";
const loadingConstructor = Vue.extend(Loading);
let instance;
const initInstance = () => {
  instance = new loadingConstructor({
    el: document.createElement("div"),
  });
};
initInstance();
let LoadingInstance = {
  showLoading: instance?.showLoading || false,
};
LoadingInstance.show = () => {
  instance.showLoading = true;
  if (Vue.prototype.$isServer) return;
  const body = document.querySelector("body");
  if (body.append) {
    body.append(instance.$el);
  } else {
    body.appendChild(instance.$el);
  }
};
LoadingInstance.hidden = () => {
  instance.showLoading = false;
};
LoadingInstance.destroy = () => {
  if (Vue.prototype.$isServer) return;
  if (instance.$el && instance.$el.parentNode) {
    instance.$el.parentNode.removeChild(instance.$el);
  }
};
export default LoadingInstance;
